<?php
namespace Home\Controller;
use Think\Controller;
/**
* 系统相关，设置
*/
class SystemController extends Controller{
	public function index(){
		$this->display();
	}
	public function getJobs(){
		$list = M('jobs')->order("first_str")->select();
		if ($list) {
			$this->ajaxReturn(array('status'=>1,'message'=>'请求成功','list'=>$list));
		}
 		$this->ajaxReturn(array('status'=>0,'message'=>'请求失败','list'=>array()));
	}
    public function redExplain(){
        $data = M('explain')->where('id=1')->find();
        if($data){
            $this->ajaxReturn(array('status'=>1,'messgae'=>'请求成功','data'=>$data));
        }
        $this->ajaxReturn(array('status'=>0,'message'=>'请求失败','data'=>array()));
    }

    public function getSign($result){

        ksort($result);

        foreach ($result as $key => $value) {

            if($key!="sign"){

               $stringA.=$key."=".$value."&";

            }

        }

        $key = M('weixin')->where(array('apple_id'=>$result['appid']))->getField('wechat_key');

        $stringSignTemp=$stringA."key=".$key;

        $sign=MD5($stringSignTemp);

        $sign=strtoupper($sign);

        return $sign;

    }
	public function Wx_pay_ajax_oks(){

        $txt=file_get_contents("php://input");
        function r_xml($strXml){
            $pos = strpos($strXml, 'xml');
                if (!$pos) {
                die("不是xml字符串！");
            }
                $obj=simplexml_load_string($strXml,'SimpleXMLElement', LIBXML_NOCDATA);
                if(is_object($obj)){
                $obj=get_object_vars($obj);
            }
            return $obj;
        }
        $xmlstring = <<<XML
<xml>
<return_code><![CDATA[SUCCESS]]></return_code>
<return_msg><![CDATA[OK]]></return_msg>
</xml>
XML;
        $result=r_xml($txt);
        // Vendor('Mywx.common');
        // $tools = new \common();
        $sign=$this->getSign($result);
        // $gai=M("Order")->where(array("l_id"=>$result["out_trade_no"]))->save(array("status"=>1));
        if($result["sign"]==$sign){
            if($result["result_code"]=="SUCCESS"){
                 //如果cate_id的值是1，则为项目捐赠，对项目做相应的操作
                $order = M('order')->where(array("deal_num"=>$result["out_trade_no"]))->find();
                if($order['type'] == 108){
                    $frize = M('frize')->where(array('user_id'=>$order['user_id']))->find();
                    $logData[0] = array(
                        'user_id'   => $order['user_id'],
                        'money'     => $order['weixin_money'] + $order['yue_money'],
                        'add_time'  => $order['add_time'],
                        'pay_type'  => $order['pay_type'],
                        'type'      => 108,
                        'activity_id'=> 0,
                        'content'   => '支付诚意金',
                    ); 
                    $logData[1] = array(
                        'user_id'   => $frize['to_user_id'],
                        'money'     => $order['weixin_money'] + $order['yue_money'],
                        'add_time'  => $order['add_time'],
                        'pay_type'  => 3,
                        'type'      => 109,
                        'activity_id'=> 0,
                        'content'   => '收到诚意金',
                    ); 
                    if($order['pay_type'] == 2){
                        M('user')->where('id='.$order['user_id'])->save(array('money'=>0));
                    }
                    $log_id = M('money_log')->add($logData[0]);
                    M('money_log')->add($logData[1]);
                    M('frize')->where(array('user_id'=>$order['user_id']))->save(array('status' => 0, 'is_drop'=>0));
                    $gai=M("order")->where('id='.$order['id'])->save(array("log_id"=>$log_id,'status'=>1));
                    //推送消息告知支付诚意金成功
                    D('Message')->sendPayChengyiMessage($order['user_id']);
                }else{
                    $logData = array(
                        'user_id'   => $order['user_id'],
                        'money'     => $order['weixin_money'] + $order['yue_money'],
                        'add_time'  => $order['add_time'],
                        'pay_type'  => $order['pay_type'],
                        'type'      => $order['type'],
                        'activity_id'=> $order['activity_id'],
                        'content'   => '发布活动付款',
                    ); 

                    $log_id = M('money_log')->add($logData);
                    $gai=M("order")->where('id='.$order['id'])->save(array("log_id"=>$log_id,'status'=>1));
                    if($order['pay_type'] == 2){
                        M('user')->where('id='.$order['user_id'])->save(array('money'=>0));
                    }
                    M('activity')->where('id='.$order['activity_id'])->save(array('is_pay'=>1));
                    M('dealing')->where('activity_id='.$order['activity_id'])->save(array('is_pay'=>1));
                    
                    //添加发布的次数
                    //查看用户是否发布次数达到5次
                    $todayTimes = M('today_times')->where('user_id='.$order['user_id'])->find();
                    if ($todayTimes) {
                        if ($todayTimes['add_time'] == strtotime(date("Y-m-d"))) {
                            M('today_times')->where('user_id')->setInc('fa_times',1);
                        }
                        if ($todayTimes['add_time'] < strtotime(date("Y-m-d"))) {
                            $saveArray = array('add_time'=>strtotime(date('Y-m-d')),'abolish_times'=>0,'fa_times'=>1,'bao_times'=>0);
                            M('today_times')->where('user_id='.$order['user_id'])->save($saveArray);
                        }
                    }
                    else{
                        //如果没有记录，则添加一条数据
                        $todayData = array(
                            'user_id'   =>$order['user_id'],
                            'fa_times'  =>0,
                            'bao_times' =>1,
                            'abolish_times' => 0,
                            'add_time'  =>strtotime(date("Y-m-d"))
                        );
                        M('today_times')->add($todayData);
                    }
                }
                
                if($gai){
                    echo $xmlstring;
                }
            } 
        }
    }
}